package com.ktjy.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TimeSpanAspect {
    private static Logger log = LoggerFactory.getLogger(LoggerAspect.class);

    /**
     * 环绕增强 - 打印方法执行耗时
     */
    public Object around(ProceedingJoinPoint jp) throws Throwable {
        log.info("[TimeSpanAspect 环绕增强-前置]记录{}方法的开始时间. ", jp.getSignature().getName());
        long start = System.currentTimeMillis();
        try {
            Object result = jp.proceed();
            log.info("[TimeSpanAspect 环绕增强-返回后通知]记录{}方法, 执行结果为{}", jp.getSignature().getName(), result);
            return result;
        } catch (Throwable e) {
            log.info("[TimeSpanAspect 环绕增强-异常]记录{}方法, 异常信息为{}", jp.getSignature().getName(), e.getMessage());
            throw e;
        } finally {
            long end = System.currentTimeMillis();
            log.info("[TimeSpanAspect 环绕增强-最终]记录{}方法, 执行耗时为{}ms", jp.getSignature().getName(), end - start);
        }
    }

}
